package com.qf.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.service.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author lenovo
 * @version V1.0
 * @Date 2021/11/19 14:26
 */
@Service
@Slf4j
public class CacheServiceImpl implements CacheService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 专门用于操作字符串类型
     */
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean set(String key,String value,Long expireSecond){

        log.info("【缓存模块】 添加数据到Redis中，并设置生存时间,key={}，value={}，expire={}", key,value,expireSecond);
        stringRedisTemplate
                .boundValueOps(key)
                .set(value, expireSecond, TimeUnit.SECONDS);
        //stringRedisTemplate
        //        .opsForValue()
        //        .set(key, value, expireSecond,TimeUnit.SECONDS);

        return true;
    }

    @Override
    public String get(String key) {
        log.info("【缓存模块】 获取value结构的数据,返回String类型   key={}",key);
        String res = stringRedisTemplate.boundValueOps(key).get();
        return res;
    }

    @Override
    public boolean saveObject(String key, Object value) {
        log.info("【缓存模块】 添加Object类型数据到Redis中，key={}，value={}", key, value);
        try {
            stringRedisTemplate
                    .boundValueOps(key)
                    .set(objectMapper.writeValueAsString(value));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Object getObject(String key) {

        log.info("【缓存模块】 获取value结构的数据,返回Object类型，key={}",key);
        String value = stringRedisTemplate
                .boundValueOps(key)
                .get();

        Object res = null;
        try {
            res = objectMapper.readValue(value, Object.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return false;
        }
        return res;
    }

    @Override
    public boolean del(String... keys) {

        log.info("【缓存模块】 删除Redis中的key   keys={}", Arrays.toString(keys));
        redisTemplate.delete(Arrays.asList(keys));
        return true;
    }

    @Override
    public boolean hMSet(String key, Map map) {
        log.info("【缓存模块】 向redis中存储HashMap结构中的数据   key={}，value={}", key, map);
        //stringRedisTemplate.boundHashOps(key).putAll(map);
        redisTemplate.opsForHash().putAll(key, map);
        return true;
    }

    @Override
    public Map hMGet(String key) {
        log.info("【缓存模块】 批量获取hash结构中的数据   key={}", key);
        //Map<Object, Object> map = stringRedisTemplate.boundHashOps(key).entries();
        Map<Object,Object> map = redisTemplate.opsForHash().entries(key);
        return map;
    }

    @Override
    public boolean saveCache(String key, String value) {
        log.info("【缓存模块】 添加数据 saveCache    key={},value={}",key,value);
        stringRedisTemplate.boundValueOps(key).set(value);
        return true;
    }

    @Override
    public Long incr(String key, long delta) {
        log.info("【缓存模块】 针对string结构实现自增   key={}，delta={}", key,delta);
        Long res = stringRedisTemplate.boundValueOps(key).increment(delta);
        return res;
    }

    @Override
    public boolean sadd(String key, String... value) {
        log.info("【缓存模块】 存储set结构数据~~   key={}，set={}", key,value);
        stringRedisTemplate.boundSetOps(key).add(value);
        return true;
    }

    @Override
    public Object hget(String key, String field) {

        log.info("【缓存模块】 根据key和field查询hash结构中的一个value   key={}，field={}", key,field);
        Object o = stringRedisTemplate.boundHashOps(key).get(field);
        return o;
    }

    @Override
    public Long hincr(String key, String field, long delta) {
        log.info("【缓存模块】 针对hash结构实现自增   key={}，field={},delta={}", key,field,delta);
        Long res = stringRedisTemplate.boundHashOps(key).increment(field, delta);
        return res;
    }

    @Override
    public Set<String> setGet(String key) {

        Set<String> set = stringRedisTemplate.opsForSet().members(key);
        return set;
    }

    @Override
    public Boolean setSet(String key, Set<String> value) {
        stringRedisTemplate.opsForSet().add(key, value.toArray(new String[]{}));
        return true;
    }

    @Override
    public Set<String> interSet(Set<String> value) {
        Set<String> set = stringRedisTemplate.opsForSet().intersect("DIRTYWORDS:ALL", value);
        return set;
    }


}
